#' ---
#' title: "Revised summary stats table, extract"
#' author: ""
#' date: "`r Sys.Date()`"
#' output: pdf_document
#' header-includes:
#'  - \usepackage{booktabs}
#'  - \usepackage{longtable}
#'  - \usepackage{array}
#'  - \usepackage{multirow}
#'  - \usepackage{wrapfig}
#'  - \usepackage{float}
#'  - \usepackage{colortbl}
#'  - \usepackage{pdflscape}
#'  - \usepackage{tabu}
#'  - \usepackage{threeparttable}
#'  - \usepackage{threeparttablex}
#'  - \usepackage[normalem]{ulem}
#'  - \usepackage{makecell}
#'  - \usepackage{dcolumn}
#'  - \usepackage{setspace}\doublespacing
#' ---


## ---- spin_code, eval = FALSE, include = FALSE ----
# spin code to output Rmd
# set knit = FALSE to generate Rmd / Rnw but not compile
# rmarkdown::render(input = here::here("code/summary_stats_revised_extract.R"), output_format = "pdf_document", clean = TRUE)

# running rmarkdown::render on summary_stats_revised.R generates 
# summary_stats_revised.tex. This code extracts middle rows of 
# summary stats tables so that N row can be customized in main doc

## ---- save_summary_stat_extract, eval = TRUE ----

# Run after summary_stats_revised.R
# Second run extracts two summary stats tables to \input to Rnw:
#   summary_stats_revised_extract_matched.tex
#   summary_stats_revised_extract_unmatched.tex

## MATCHED
# read in table output    
x <- readLines(here("code/summary_stats_revised_extract_matched_full_table.tex"), warn = FALSE)

# identify start and end of main results of matched data
start <- which(str_detect(x, "\\% Black"))[1]
end   <- which(str_detect(x, "log\\(Population\\)"))[1]

# extract relevant lines
y     <- x[start:end] 

# write extracted lines to new .tex file for input to main doc
cat(y, file=here("code/summary_stats_revised_extract_matched.tex"), sep="\n" )


## UNMATCHED
# read in table output    
x <- readLines(here("code/summary_stats_revised_extract_unmatched_full_table.tex"), warn = FALSE)

# identify start and end of main results of unmatched data
start <- which(str_detect(x, "\\% Black"))[1]
end   <- which(str_detect(x, "log\\(Population\\)"))[1]

# extract relevant lines
y     <- x[start:end] 

# write extracted lines to new .tex file for input to main doc
cat(y, file=here("code/summary_stats_revised_extract_unmatched.tex"), sep="\n" )


